<?php
namespace Crm\Domain;

use Crm\Common\MetaData as MetaData;

class Shop
{
    /**
     * pdo连接。
     *
     * @var object
     * @access pdo
     */
    public $pdo;

    public $metaData;
    public $baseCode;
    public $city_code;
    public $provinceCode;

    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
        $this->baseCode = (object)\PhalApi\DI()->config->get('basecode');
        $this->province_code = \PhalApi\DI()->config->get('app.provinceCode');//省
        $this->city_code = \PhalApi\DI()->config->get('app.cityCode');//城市

        $this->metaData = new MetaData();
    }

    /* 商家列表 */
    public function search($mall_id,$data){
        $order = isset($data['order']) ? $data['order'] : 'sort desc';
        $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 30;
        $offset = 0;
        $subPages=5;//每次显示的页数
        $currentPage = isset($data['page']) ? (int)$data['page'] : 0;
        if($currentPage>0) $offset=($currentPage-1)*$pageSize;

        $where = 'a.flag=1 AND a.mall_id='.$mall_id.' ';
        if(isset($data['key']) && !empty($data['key'])){
            $where .= " AND a.name like '%".$data['code']."%'";
        }
        if(isset($data['type']) && !empty($data['type'])) {
            $where .= " AND a.type = " . $data['type'];
        }

        $order = " order by $order";
        $limit = " limit $offset,$pageSize";
        //获取商家列表
        $sql = "select * from mk_shop as a where ".$where.$order.$limit;
        $shops = $this->pdo->getAll($sql);
        foreach ($shops as $key=>$item){
            $shops[$key]['img_url_s'] = str_replace('.','_s.',$item['img_url']);
            $shops[$key]['img_url_m'] = str_replace('.','_m.',$item['img_url']);
            //获取图片列表
            $sql = "select name,des,url from sys_attach where flag=1 and action='shop' and action_id=".$item['id'];
            $shops[$key]['pics'] = $this->pdo->getAll($sql);
            foreach ($shops[$key]['pics'] as $k=>$v){
                $shops[$key]['pics'][$k]['url_s'] = str_replace('.','_s.',$v['url']);
                $shops[$key]['pics'][$k]['url_m'] = str_replace('.','_m.',$v['url']);
            }
            $shops[$key]['rating_star_half'] = 0;
            if($item['rating_star']!=ceil($item['rating_star'])) {$shops[$key]['rating_star_half'] = 1;}
        }
        return $shops;
    }

    /* 商家详情 */
    public function detail($shop_id){
        $sql = "select * from mk_shop as a where id={$shop_id} and flag=1";
        $rlt = $this->pdo->getRow($sql);
        $rlt['img_url_s'] = str_replace('.','_s.',$rlt['img_url']);
        $rlt['img_url_m'] = str_replace('.','_m.',$rlt['img_url']);
        //获取相册
        $rlt['pics'] = $this->photo($shop_id);

        //处理描述
        $arr=explode("\n",$rlt['des']);
        $pstr='';
        foreach ($arr as $value){
            $pstr .='<p>'.$value.'</p>';
        }
        $rlt['pdes'] = $pstr;

        //处理评星
        $rlt['rating_star_half'] = 0;
        if($rlt['rating_star']!=ceil($rlt['rating_star'])) {$rlt['rating_star_half'] = 1;}
        return $rlt;
    }

    /* 商家相册 */
    public function photo($shop_id){
        $sql = "select name,url,code,title from sys_attach as a where flag=1 and action='shop' and action_id={$shop_id} ORDER BY sort desc";
        $rlt = $this->pdo->getAll($sql);
        foreach ($rlt as $key=>$item) {
            $item['url_s'] = str_replace('.', '_s.', $item['url']);
            $item['url_m'] = str_replace('.', '_m.', $item['url']);
            if($item['code']==1)$pics['swiperPics'][] = $item;
            if($item['code']==2)$pics['photoPics'][] = $item;
        }
        //获取相册
        return $pics;
    }

    /* 商家负责人 */
    public function owner($shop_id){
        $sql = "select u.id AS user_id,u.name,
                a.id AS shop_id,a.name AS shop_name 
                from mk_shop as a LEFT JOIN wx_user as u on a.owner_id=u.id where a.id={$shop_id}";
        $owner = $this->pdo->getRow($sql);
        return $owner;
    }


}

